library(data.table)
load("all_data.rda")
load("text_messages.rda")
load("chat_coding.RData")
sender_data <- all_data[Type == 1]
receiver_data <- all_data[Type == 0]
pair_data <- merge(
  sender_data[, .(
    sender_payoff = (320 + payoff) / 20,
    action = Action, target = Target,
    condition = Condition, bias = Bias,
    sender_id = ID, group = Group, period = Period,
    session = SessionID)],
  receiver_data[, .(
    receiver_payoff = (320 + payoff) / 20,
    receiver_id = ID, group = Group, period = Period,
    session = SessionID)],
  by = c("group", "period", "session"))
pair_data[bias == "High",
  eqm_action_proximity := -as.double(abs(action))]
pair_data[bias == "Low" & target < -80,
  eqm_action_proximity := -as.double(abs(action - -90))]
pair_data[bias == "Low" & target > -80,
  eqm_action_proximity := -as.double(abs(action - 10))]
pair_data[, time := (period - 1) / 29]
pair_data[, target_action_proximity := -abs(target - action)]
pair_data[, pareto := as.numeric(
  action >= target & action <= target + ifelse(bias == "Low", 40, 80)
)]
chat_coding[SessionID == "161021_1147", Session := 5]
chat_coding[SessionID == "161021_1359", Session := 6]
chat_coding[SessionID == "161017_1350", Session := 3]
chat_coding[, id := Session * 100 + Subject]
chat_coding[category == "Uninformative", category := "Empty"]
text_messages <- merge(chat_coding,
  pair_data[condition == "Text",
    .(SessionID = session, Period = period, id = sender_id,
      Target = target,
      target.01 = target / 100, time, Bias = bias)],
  by = c("SessionID", "Period", "id"),
  sort = FALSE)
payoffs <- as.numeric(c(
  fread("161017_0949_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay],
  fread("161017_1141_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay],
  fread("161017_1350_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay],
  fread("161021_0918_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay],
  fread("161021_1147_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay],
  fread("161021_1359_2_subjects.csv")[Group != 0 & Period == 30,
    finalpay]))
pair_data <- merge(
  pair_data,
  text_messages[!is.na(Message_number),
    .(session = SessionID, period = Period,
      sender_id = Session * 100 + Subject, Message_number)],
  all.x = TRUE,
  sort = FALSE,
  by = c("session", "period", "sender_id"))
pair_data <- merge(
  pair_data,
  sender_data[Condition == "Numeric", .(
    session = SessionID, period = Period, sender_id = Session * 100 + Subject,
    Message
  )],
  all.x = TRUE,
  sort = FALSE,
  by = c("session", "period", "sender_id"))
pair_data[condition == "Numeric",
  target_message_proximity := -abs(target - Message)]
pair_data[condition == "Text" & !is.na(Message_number),
  target_message_proximity := -abs(target - Message_number)]
pair_data[condition == "Numeric",
  message_num := Message]
pair_data[condition == "Text" & !is.na(Message_number),
  message_num := Message_number]
chat_coding[, category := factor(category, levels = c(
  "Interval",
  "Uninformative",
  "Noisy",
  "Precise"
))]
chat_coding[, Shift := "High Shift"]
chat_coding[Period >= 16, Shift := "Low Shift"]
save(sender_data, receiver_data, text_messages, payoffs, pair_data, chat_coding,
  file = "analysis_data.RData")